<?php

namespace Admin\Controller;

use \Admin\Controller\AdminController;

/*
 * 分组管理
 */

class GroupController extends AdminController {

    /**
     * 分组列表
     */
    public function index() {
        $where = array();
        $where['news_title'] = array('like', '%' . I("title") . '%');
        $eid = I("eid", 0, 'intval');
        $eventWhere = array();
        if ($eid > 0) {
            $eventWhere['ner.event_id'] = $eid;
            $this->assign('eid', $eid);
        }

        $list = M('News')
                ->where($where)
                ->order("create_time desc")
                ->select();

        $new_ids = M('NewsEventRalt')->where(array('event_id' => $eid))->select();

        $ids = '';
        foreach ($new_ids as $id) {
            $ids .= $id['news_id'] . ',';
        }
        $ids = substr($ids, 0, strlen($ids) - 1);

        $newEventList = M()->table("im_news_event_ralt ner")
                ->field("ner.*,e.event_name")
                ->join("left join im_event as e ON ner.event_id = e.event_id")
                ->where(array('news_id' => array('in', explode(',', $ids))))
                ->select();

        $result = array();
        foreach ($list as $val) {
            $event = '';
            foreach ($newEventList as $row) {
                if ($val['news_id'] == $row['news_id']) {
                    $event .= $row['event_name'] . ',';
                }
            }
            if (!empty($event)) {
                $newstr = substr($event, 0, strlen($event) - 1);
                $val['event'] = $newstr;
                $result[] = $val;
            }
        }

        $eventList = M("Event")->select();

        $this->assign('_list', $result);
        $this->assign("eventList", $eventList);
        $this->meta_title = '资讯管理';
        $this->display();
    }

    /**
     * 添加资讯
     */
    public function add() {
        if (IS_POST) {
            $event_id = I('event_id');

            $data['news_title'] = I('news_title');
            $data['news_img'] = I('news_img');
            $data['news_content'] = I('news_content');

            if (empty($data['news_title']) || empty($data['news_img']) || empty($data['news_content']) || empty($event_id)) {
                $this->error('必填项必须填写');
            }

            $res = M("News")->add($data);

            if ($res) {
                $num = 0;
                foreach ($event_id as $val) {
                    $n['news_id'] = $res;
                    $n['event_id'] = $val;
                    $ret = M('NewsEventRalt')->add($n);
                    if (!empty($ret)) {
                        $num++;
                    }
                }
                if ($num > 0) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败', U('add'));
                }
            } else {
                $this->error('添加失败', U('add'));
            }
        } else {
            $eventList = M("Event")->select();
            $this->assign("eventList", $eventList);
            $this->meta_title = '添加资讯';
            $this->display();
        }
    }

    /**
     * 编辑资讯
     */
    public function edit() {
        if (IS_POST) {
            $nid = I("nid", 0, 'intval');

            $data['news_title'] = I('news_title');
            $data['news_img'] = I('news_img');
            $data['news_content'] = I('news_content');

            $event_id = I('event_id');

            $res = M("News")->where(array('news_id' => $nid))->save($data);
            if ($res) {
                $ret = M('NewsEventRalt')->where(array('news_id' => $nid))->delete();
                $num = 0;
                foreach ($event_id as $val) {
                    $n['news_id'] = $nid;
                    $n['event_id'] = $val;
                    $ret = M('NewsEventRalt')->add($n);
                    if (!empty($ret)) {
                        $num++;
                    }
                }
                if ($num > 0) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败', U('add'));
                }
            } else {
                $ret = M('NewsEventRalt')->where(array('news_id' => $nid))->delete();
                $num = 0;
                foreach ($event_id as $val) {
                    $n['news_id'] = $nid;
                    $n['event_id'] = $val;
                    $ret = M('NewsEventRalt')->add($n);
                    if (!empty($ret)) {
                        $num++;
                    }
                }
                if ($num > 0) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败', U('add'));
                }
            }
        } else {
            $nid = I("nid");
            $data = M("News")->where(array('news_id' => $nid))->find();
            $this->meta_title = '编辑资讯';

            $eventList = M("Event")->select();
            $this->assign("eventList", $eventList);

            $newsEvent = M('NewsEventRalt')->where(array('news_id' => $nid))->select();
            $this->assign("newsEvent", $newsEvent);
            $this->assign('data', $data);
            $this->display("add");
        }
    }

    /**
     * 删除资讯
     */
    public function del() {
        $ids = I('nid');
        $res = M("News")->where(array('news_id' => array('in', explode(',', $ids))))->delete();
        $ret = M("NewsEventRalt")->where(array('news_id' => array('in', explode(',', $ids))))->delete();
        if ($res && $ret) {
            $this->success('删除成功', U('index'));
        } else {
            $this->error('删除失败');
        }
    }

}
